package user;

import utils.JsonOut;
import utils.MysqlUtils;
import utils.VerifyEmail;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.Random;

@WebServlet(name = "ForgetPasswordServlet")
public class ForgetPasswordServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 检查email字段
        // 发送激活邮件

        String email = request.getParameter("email");

        String verify = request.getParameter("verify");

        HttpSession session = request.getSession();
        Object captchaAttr = session.getAttribute("validation_code");

        PrintWriter out = response.getWriter();
        if (captchaAttr == null){
            out.println(JsonOut.error(201, "captcha_empty"));
            return;
        }
        if (email == null || email.isEmpty()){
            out.println(JsonOut.error(201, "email_empty"));
            return;
        }
        if (verify == null || verify.isEmpty()){
            out.println(JsonOut.error(201, "verify_empty"));
            return;
        }

        String captcha = captchaAttr.toString();
        // 忽略大小写
        verify = verify.toLowerCase();
        captcha = captcha.toLowerCase();
        if (!verify.equals(captcha)){
            out.println(JsonOut.error(103, "verify"));
            return;
        }

        ResultSet resultSet = (new MysqlUtils()).getEmailVerify(email);

        if (resultSet == null){
            out.println(JsonOut.error(101, "not_exist"));
            return;
        }

        try{
            String emailVtime = resultSet.getString(2);
            long time = Long.parseLong(emailVtime);
            if (System.currentTimeMillis() - time < 60000){
                out.println(JsonOut.error(109, "too_frequent"));
                return;
            }
        }catch (Exception e){
            out.println(JsonOut.error(101, "not_exist"));
            return;
        }

        String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 32; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }

        String emailError = "";
        if ((new MysqlUtils()).updateEmailVcode(email, sb.toString())){
            session.setAttribute("forget_psw_email", email);
            if(!VerifyEmail.send(email, "forget", sb.toString())) emailError = "_but_email_error";
            out.println(JsonOut.error(0, "request_success"+emailError));
        }else
            out.println(JsonOut.error(500, "request_failed"));



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println(JsonOut.error(403, "forbidden"));
    }
}
